#! /bin/bash

gopath=$(go env | grep GOPATH | cut -d "=" -f 2 | sed 's/\"//g')

if [ "$gopath" = "" ]
then
	gopath=/home/GoFile
	echo "gopath err , \"/home/GoFile\" as the gopath"
	exit 1
fi

projectpath=$gopath/src/gitee.com/wanttobeamaster/gridbase
targetfile=$gopath/src/gitee.com/wanttobeamaster/ela-up

while read -p "are you sure to update all server! [yes/no]  " answer
do
	if [ $answer = "yes" ];then
		break 2
	fi
	if [ $answer = "no" ]; then
		exit 1
	fi
done

while read -p "only clear all server's log and data [yes / no] " onlyclearlog 
do
	if [ $onlyclearlog = "yes" ] ; then 
		break 2
	fi
	if [ $onlyclearlog = "no" ] ; then
		break 2
	fi
done

Localhost="1"

# read host and transport binary file to target host
while read line
do
ip=$(echo $line | cut -d" " -f1)
passwd=$(echo $line | cut -d" " -f2)
$targetfile/exp.sh $ip $passwd $onlyclearlog $targetfile/download.sh
if [ $Localhost = "1" ]; then
	if [ $onlyclearlog = "no" ]; then
		$targetfile/make-binary-file.sh
	fi
	Localhost="0"
else
	if [ $onlyclearlog = "no" ]; then
		sshpass -p $passwd scp -r $projectpath/dist root@$ip:$projectpath/
	fi
fi
done < /home/hosts


